<template>
    <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" width="30%"
               :close-on-click-modal="false" draggable>
        <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px"
                 v-loading="loading">
            <el-row :gutter="24">
                <el-col :span="24" class="mb20">
                    <el-form-item :label="$t('poheader.supplierName')" prop="supplierCode">
                        <el-input v-model="form.supplierCode" placeholder="请输入供应商代码" readonly/>
                    </el-form-item>
                </el-col>

                <el-col :span="24" class="mb20">
                    <el-form-item :label="$t('SrmPoHeaderGroup.phoneNumber')" prop="phoneNumber">
                        <el-input v-model="form.phoneNumber" placeholder="请输入电话号码"/>
                    </el-form-item>
                </el-col>

                <el-col :span="24" class="mb20">
                    <el-form-item :label="$t('SrmPoHeaderGroup.deliveryRemark')" prop="deliveryRemark">
                        <el-input v-model="form.deliveryRemark"  type="textarea" placeholder="请输入交货备注"/>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <template #footer>
        <span class="dialog-footer">
            <el-button type="primary" icon="check" @click="onSubmit" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
            <el-button icon="close" @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
        </span>
        </template>
    </el-dialog>
</template>

<script setup lang="ts" name="SrmPoheaderDialog">
import {useMessage} from "/@/hooks/message";
import {createdAsnInfo, getObj} from '/@/api/platform/srm/srmPoheader'
import {Session} from "/@/utils/storage";

const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典

// 提交表单数据
const form = reactive({
    id: '',
    poCode: '',
    supplierId: '',
    supplierCode: Session.get("user_name")||'',
    supplierName: '',
    status: '',
    purDate: '',
    k3CloseStatus: '',
    k3CancelStatus: '',
    k3CreationTime: '',
    k3Id: '',
    creationTime: '',
    createUserNo: '',
    createUserName: '',
    lastModifyTime: '',
    lastModifyUserName: '',
    lastModifyUserNo: '',
    attribute1: '',
    attribute2: '',
    attribute3: '',
    deliveryRemark:'',
    phoneNumber:'',
    materialDetailVos:[],
});

// 定义校验规则
const dataRules = ref({
  // phoneNumber: [
  //   { required: true, message: '请输入电话号码', trigger: 'blur' },
  //   {
  //     pattern: /^1[3-9]\d{9}$/,
  //     message: '请输入正确的手机号码',
  //     trigger: 'blur'
  //   }
  // ]
})

// 打开弹窗
const openDialog = (rows) => {
    form.materialDetailVos = rows
    visible.value = true
    form.id = ''

    // 重置表单数据
    nextTick(() => {
        dataFormRef.value?.resetFields();
    });

};

// 提交
const onSubmit = async () => {
    const valid = await dataFormRef.value.validate().catch(() => {
    });
    if (!valid) return false;

    try {
        loading.value = true;
        await createdAsnInfo(form);
        useMessage().success('添加成功');
        visible.value = false;
        emit('refresh');
    } catch (err: any) {
        useMessage().error(err.msg);
    } finally {
        loading.value = false;
    }
};


// 初始化表单数据
const getsrmPoheaderData = (id: string) => {
    // 获取数据
    loading.value = true
    getObj(id).then((res: any) => {
        Object.assign(form, res.data)
    }).finally(() => {
        loading.value = false
    })
};

// 暴露变量
defineExpose({
    openDialog
});
</script>